import useUserStore from '@/stores/user.js'
import router from '@/router/index.js'
import { titleSetting } from '@/utils/index.js'
import { TEACHER } from './enum/userType'
const whiteList = ['/login', '/share-middle', '/tree/history', '/tree/docs'] // no redirect whitelist
titleSetting()
router.beforeEach(async (to, from, next) => {
  titleSetting()
  const userStore = useUserStore()
  const token = userStore.getToken
  if (token) {
    if (to.path === '/login') {
      next({ path: '/' })
    } else if (to.path.includes('/meeting')) {
      if (!userStore.info.type) {
        await userStore.getInfo()
      }
      if (userStore.info.type == TEACHER) {
        next()
      } else {
        next('/404')
      }
    } else {
      next()
    }
  } else {
    if (whiteList.indexOf(to.path) === -1) {
      // 需要认证token的路由，跳转到登录界面
      next(`/login?redirect=${to.path}`)
    } else {
      next()
    }
  }
})
